package shooting.ui
{
	import shooting.event.UnitEvents;
	import shooting.ui.unit.IUnit;

	public class UnitContainer implements IObjectContainer
	{
		private var unitArray:Array;
		
		public function UnitContainer()
		{
			unitArray = new Array();
		}

		public function get length():Number
		{
			return unitArray.length;
		}
		
		public function render():void
		{
			var i:uint;
			var leng:uint = unitArray.length;
			for( i = leng; i > 0;)
			{
				i--;
				var unit:Object = unitArray[i];
				unit.render();	
			}
		}
		
		public function getList():Array
		{
			return unitArray;
		}
		
		public function addUnit(unit:IUnit):void
		{
			unit.addEventListener(UnitEvents.UNIT_REMOVE, removeUint);
			unitArray.push(unit);
		}
		
		private function removeUint(e:UnitEvents):void
		{
			var i:uint;
			var leng:uint = length;
			for(i = 0;i < leng; i ++ )
			{
				if(unitArray[i] == e.unit)
				{
					unitArray.splice(i,1);
				}
			}
		}
	}
}